export default {
  props: {
    value: [Array, Object],
    relModule: String, // 关联模块——取当前路由作为关联模块
    relType: String, // 关联的实体类名
    relField: String, // 关联的实体类字段名
    relId: String, // 关联的实体数据ID
    multiple: Boolean,
    limit: {
      type: Number,
      default: 3
    },
    disabled: Boolean,
    accept: String,
  },
  data() {
    return {
      action: '/jsofted/file/upload',
      fileList: [],
      params: {},
      headers: {},
      max: this.limit,
      init: window.$debounce(this.initMe, 300),
      clazz: '',
      refreshClazz: window.$debounce(this._refreshClazz),
      showViewer: false
    };
  },
  watch: {
    value() {
      this.init()
    },
    fileList() {
      this.refreshClazz()
    }
  },
  computed: {
    imageUrls() {
      let array = []
      this.fileList.forEach((f) => {
        array.push(f.url)
      })
      // console.log('imageUrls: ', JSON.stringify(array))
      return array
    },
  },
  mounted() {
    this.init()
  },
  methods: {
    initMe() {
      this.max = this.multiple ? this.limit : 1
      if (this.value) {
        let newList = []
        if (this.multiple) {
          newList = this.value
        } else {
          newList = [this.value]
        }
        let notEquals = false
        if (this.fileList.length != newList.length) {
          notEquals = true
        } else {
          for (let i = 0; i < this.fileList.length; i++) {
            if (this.fileList[i].id !== newList[i].id) {
              notEquals = true
              break
            }
          }
        }
        if (notEquals) {
          this.fileList = newList
        }
      } else {
        this.fileList = []
      }
      // console.log('init: %s', JSON.stringify(this.fileList))
      this.params = {
        relModule: this.relModule || this.$route.path,
        relType: this.relType || '',
        relField: this.relField || '',
        relId: this.relId || '',
      }
      this.headers = {
        LOGIN_TOKEN: this.$loginToken
      }
    },
    _refreshClazz() {
      if (this.fileList.length >= this.max) {
        this.clazz = 'exceed'
      } else {
        this.clazz = ''
      }
    },
    doPreview() { // file
      this.showViewer = true
    },
    doCloseViewer() {
      this.showViewer = false
    },
    doRemove(file, fileList) {
      this.removeFile(file)
      this.fileList = fileList
      this.emit()
    },
    doBeforeUpload() { // file
      // console.log('doBeforeUpload: fileType=%s, fileSize=%s', file.type, file.size)
      // const isJPG = file.type === 'image/jpeg';
    },
    doSuccess(response) { // response, file, fileList
      if (response.success) {
        this.fileList.push(response.data)
      }
      this.emit()
    },
    doError() { // err, file, fileList
      // console.log('doError: err=', JSON.stringify(err))
      // console.log('doError: file=', JSON.stringify(file))
      // console.log('doError: fileList=', JSON.stringify(fileList))
    },
    doProgress() { // event, file, fileList
      // console.log('doProgress: event=', JSON.stringify(event))
      // console.log('doProgress: file=', JSON.stringify(file))
      // console.log('doProgress: fileList=', JSON.stringify(fileList))
    },
    doChange() { // file, fileList
      // console.log('doChange: file=', JSON.stringify(file))
      // console.log('doChange: fileList=', JSON.stringify(fileList))
    },
    doBeforeRemove() { // file, fileList
      // console.log('doBeforeRemove: file=', JSON.stringify(file))
      // console.log('doBeforeRemove: fileList=', JSON.stringify(fileList))
    },
    doExceed() { // file, fileList
      // console.log('doExceed..')
    },
    removeFile(file) {
      if (file.id) {
        this.$http.get('/jsofted/file/remove/' + file.id).then((rs) => {
          const result = rs.data
          if (!result.success) {
            this.$message.error(result.message)
          }
        })
      }
    },
    emit() {
      let datas = []
      this.fileList.forEach((f) => {
        datas.push({
          id: f.id,
          name: f.name,
          url: f.url
        })
      })
      // console.log('emit: datas=', JSON.stringify(datas))
      if (this.multiple) {
        this.$emit('input', datas)
      } else if (datas.length) {
        this.$emit('input', datas[0])
      } else {
        this.$emit('input', null)
      }
    },
  },
};